package com.peng.one.push.huawei.hmsagents;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
import com.peng.one.push.cache.OnePushCache;
import com.peng.one.push.core.IPushClient;
import com.peng.one.push.log.OneLog;

/**
 * HMS推送客户端
 * Created by pyt on 2017/5/15.
 */

public class HMSPushClient implements IPushClient {

    private static final String TAG = "HMSPushClient";
    private Context context;

    @Override
    public void initContext(Context context) {
        this.context = context.getApplicationContext();
        OneLog.i("huawei-hmsagents initContext is ");
    }

    private void connect() {
        Log.e(TAG, "connect ");
        getToken();
    }

    private void getToken() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    // read from agconnect-services.json
                    String appId = "Please enter your App_Id from agconnect-services.json ";
                    String token = HmsInstanceId.getInstance(context).getToken(appId, "HCM");
                    Log.e(TAG, "get token:" + token);
                } catch (ApiException e) {
                    Log.e(TAG, "get token failed, " + e);
                }
            }
        }).start();
//        HMSAgent.Push.getToken(new GetTokenHandler() {
//            @Override
//            public void onResult(int rst) {
//                OneLog.i("huawei-hmsagents getToken onResult=" + rst);
//            }
//        });
    }

    @Override
    public void register() {
        connect();
    }

    @Override
    public void unRegister() {
        final String token = OnePushCache.getToken(context);
        if (!TextUtils.isEmpty(token)) {
//            HMSAgent.Push.deleteToken(token, new DeleteTokenHandler() {
//                @Override
//                public void onResult(int rst) {
//                    OneLog.i("huawei-hmsagents deleteToken onResult=" + rst);
//                }
//            });
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // read from agconnect-services.json
                        String appId = "Please enter your App_Id from agconnect-services.json ";
                        HmsInstanceId.getInstance(context).deleteToken(appId, "HCM");
                        Log.i(TAG, "deleteToken success.");
                    } catch (ApiException e) {
                        Log.e(TAG, "deleteToken failed." + e);
                    }
                }
            }).start();
        }
    }

    @Override
    public void bindAlias(String alias) {
        //hua wei push is not support bind account
    }

    @Override
    public void unBindAlias(String alias) {
        //hua wei push is not support unbind account
    }

    /**
     * 注意：切换到HMS Push后，原来的setTag/getTag/deleteTag功能暂时不可用，
     * 老版本push sdk现在也无法上报新增标签，目前在华为开发者联盟portal上面仅支持根据存量的标签推送消息。
     * HMS SDK会在后续版本实现该功能。
     * */
    @Override
    public void addTag(String tag) {
//    if (TextUtils.isEmpty(tag)) {
//      return;
//    }
//    HMSAgent.Push.setTags(huaweiApiClient, Collections.singletonMap(tag, tag));
    }

    @Override
    public void deleteTag(String tag) {
//    if (TextUtils.isEmpty(tag)) {
//      return;
//    }
//    HuaweiPush.HuaweiPushApi.deleteTags(huaweiApiClient, Collections.singletonList(tag));
    }
}
